package com.example.rk.controller;
import com.example.rk.common.MyPageInfo;
import com.example.rk.common.SessionParameter;
import com.example.rk.entity.Admin;
import com.example.rk.entity.User;

import com.example.rk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    @RequestMapping("/delete/{id}")
    public String delete(HttpServletRequest request, @PathVariable("id") int id)
    {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        service.deleteById(id);
        if (admin.getType().equals("学生")) {
            return "redirect:/user/xslist1";
        }else if(admin.getType().equals("公司"))
        {
            return "redirect:/user/gslist";
        }else
        {
            return "redirect:/renko/main";
        }
 }
    @RequestMapping("/doAdd1/{id}")
    public String doAdd1(HttpServletRequest request,@PathVariable("id")int id){
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        User user=new User();
        int cid=service.getcid(id);
        String name=service.getname(id);
        String position=service.getposition2(id);
        user.setGsphone(service.getgsphone(cid));
        user.setCid(cid);
        user.setGsname(name);
        user.setXsname(admin.getName());
        user.setSid(admin.getId());
        user.setPosition(position);
        user.setXsphone(admin.getPhone());
        service.doAdd1(user);
        return "redirect:/company/list";
    }
    @RequestMapping("/doAdd2/{id}")
    public String doAdd2(HttpServletRequest request, @PathVariable("id") int id){
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);

        User user=new User();
        int sid=service.getsid(id);
        String information=service.getinformation(id);
        String position=service.getposition(id);
        user.setXsphone(service.getxsphone(sid));
        user.setCid(admin.getId());
        user.setSid(sid);
        user.setXsname(information);
        user.setGsname(admin.getName());
        user.setPosition(position);
        user.setGsphone(admin.getPhone());
        service.doAdd2(user);
        return "redirect:/job/list";
    }

  @RequestMapping("/xslist")
    public ModelAndView main1(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/xs/userlist");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearch1(request,admin.getId());
        mv.addObject("searchgsname",request.getParameter("gsname"));
       mv.addObject("searchposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }
    @RequestMapping("/xslist1")
    public ModelAndView mainxs(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/xs/userlist1");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearchxs(request,admin.getId());
        mv.addObject("searchgsname",request.getParameter("gsname"));
        mv.addObject("searchposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }
    @RequestMapping("/xslist2")
    public ModelAndView mainxs2(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/xs/userlist2");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearchxs1(request,admin.getId());
        mv.addObject("searchgsname",request.getParameter("gsname"));
        mv.addObject("searchposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }
    @RequestMapping("/gslist")
    public ModelAndView main2(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/gs/userlist");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearch2(request,admin.getId());
        mv.addObject("searchxsname",request.getParameter("xsname"));
        mv.addObject("searchposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }
    @RequestMapping("/gslist1")
    public ModelAndView maings(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/gs/userlist1");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearchgs(request,admin.getId());
        mv.addObject("searchxsname",request.getParameter("xsname"));
        mv.addObject("searchdposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }
    @RequestMapping("/gslist2")
    public ModelAndView maings2(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/gs/userlist2");
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo= service.findBySearchgs1(request,admin.getId());
        mv.addObject("searchxsname",request.getParameter("xsname"));
        mv.addObject("searchdposition",request.getParameter("position"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }

    @RequestMapping("/doEdit1/{id}")
    public String doEdit1(HttpServletRequest request, @PathVariable("id") int id)
    {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        service.doEdit1(id);
        if (admin.getType().equals("学生")) {
            return"redirect:/user/xslist1";
        }else if(admin.getType().equals("公司"))
        {
            return "redirect:/user/gslist1";
        }else
        {
            return "redirect:/renko/main";
        }
    }
    @RequestMapping("/doEdit2/{id}")
    public String doEdit2(HttpServletRequest request, @PathVariable("id") int id)
    {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        service.doEdit2(id);
        if (admin.getType().equals("学生")) {
            return"redirect:/user/xslist1";
        }else if(admin.getType().equals("公司"))
        {
            return "redirect:/user/gslist1";
        }else
        {
            return "redirect:/renko/main";
        }
    }
}

